﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AdvancedApi.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace AdvancedApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private readonly ILogger<WeatherForecastController> _logger;
        private readonly IOptions<LearningOptions> _learningOptions;
        private readonly IOptionsSnapshot<LearningOptions> _learningOptionsSnapshot;
        private readonly IOptionsMonitor<LearningOptions> _learningOptionsMonitor;
        private readonly IPostConfigureOptions<LearningOptions> _postConfigureOptions;
        private readonly IConfiguration _configuration;

        private readonly IOptionsSnapshot<ThemeOptions> _optionsSnapshot;

        private readonly IOptionsSnapshot<AppInfoOptions> _appinfoOptions;

        public WeatherForecastController(ILogger<WeatherForecastController> logger,
            IOptions<LearningOptions> learningOptions,
            IOptionsSnapshot<LearningOptions> learningOptionsSnapshot,
            IOptionsMonitor<LearningOptions> learningOptionsMonitor,
            IConfiguration configuration, 
            IPostConfigureOptions<LearningOptions> postConfigureOptions,
            IOptionsSnapshot<ThemeOptions> optionsSnapshot, IOptionsSnapshot<AppInfoOptions> appinfoOptions
    )
        {
            _logger = logger;
            _learningOptions = learningOptions;
            _learningOptionsSnapshot = learningOptionsSnapshot;
            _learningOptionsMonitor = learningOptionsMonitor;
            _configuration = configuration;
            _postConfigureOptions = postConfigureOptions;
            _optionsSnapshot = optionsSnapshot;
            _appinfoOptions = appinfoOptions;
            _learningOptionsMonitor.OnChange((options, value) =>
            {
                _logger.LogInformation($"OnChnage => {JsonConvert.SerializeObject(options)}");
            });
        }

        [HttpGet("{action}")]
        public ActionResult GetOptions()
        {
            var builder = new StringBuilder();
            builder.AppendLine("learningOptions:");
            builder.AppendLine(JsonConvert.SerializeObject(_learningOptions.Value));
            builder.AppendLine("learningOptionsSnapshot:");
            builder.AppendLine(JsonConvert.SerializeObject(_learningOptionsSnapshot.Value));
            builder.AppendLine("learningOptionsMonitor:");
            builder.AppendLine(JsonConvert.SerializeObject(_learningOptionsMonitor.CurrentValue));


            builder.AppendLine("Dark:");
            builder.AppendLine(JsonConvert.SerializeObject(_optionsSnapshot.Get("DarkTheme")));

            builder.AppendLine("white:");
            builder.AppendLine(JsonConvert.SerializeObject(_optionsSnapshot.Get("WhiteTheme")));
           

            builder.AppendLine("redis配置源:");
            builder.AppendLine(JsonConvert.SerializeObject(_appinfoOptions.Value));
            return Content(builder.ToString());
        }
    }
}
